package mapper.sys;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.BaseMapper;

import beans.sys.UserBean;

public interface UserMapper<T> extends BaseMapper<T> {
	/**
	 * 登录
	 * @param userName
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@Select("select * from sys_user where account=#{un} and password=#{pw} and isuse='1'")
	@Results({
		@Result(id=true,property="id",column="id",javaType=String.class),
		@Result(property="username",column="username",javaType=String.class),
		@Result(property="password",column="password",javaType=String.class),
		@Result(property="account",column="account",javaType=String.class),
		@Result(property="job",column="job",javaType=String.class),
		@Result(property="adduser",column="adduser",javaType=String.class),
		@Result(property="remark",column="remark",javaType=String.class),
		@Result(property="isuse",column="isuse",javaType=String.class),
		@Result(property="addtime",column="addtime",javaType=Date.class),
		@Result(property="wxnumber",column="wxnumber",javaType=String.class),
		@Result(property="tel",column="tel",javaType=String.class),
		@Result(property="type",column="type",javaType=String.class),
		@Result(property="wxopenid",column="wxopenid",javaType=String.class)
	})
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public UserBean login(@Param("un")String username,@Param("pw")String password);
	
	/**
	 * 新增用戶
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@Insert("INSERT INTO sys_user values (#{id},#{username},#{account},#{password},#{job},#{remark},#{adduser},#{addtime},#{isuse},#{wxnumber},#{tel},#{type},#{wxopenid})")
	public void add(@Param("m")UserBean m);

	@Update("UPDATE sys_user SET username=#{username},account=#{account},job=#{job},remark=#{remark},isuse=#{isuse},wxnumber=#{wxnumber},tel=#{tel},wxopenid=#{wxopenid} WHERE id=#{id}")
	public void update(@Param("m")UserBean m);
	
	/**
	 * 根据id查询用户信息
	 * @param id
	 * @return
	 * @throws Exception
	 */

	@Select(" select * from sys_user where id=#{id}")
	@Results({
		@Result(id=true,property="id",column="id",javaType=String.class),
		@Result(property="username",column="username",javaType=String.class),
		@Result(property="password",column="password",javaType=String.class),
		@Result(property="account",column="account",javaType=String.class),
		@Result(property="job",column="job",javaType=String.class),
		@Result(property="adduser",column="adduser",javaType=String.class),
		@Result(property="remark",column="remark",javaType=String.class),
		@Result(property="isuse",column="isuse",javaType=String.class),
		@Result(property="addtime",column="addtime",javaType=Date.class),
		@Result(property="wxnumber",column="wxnumber",javaType=String.class),
		@Result(property="tel",column="tel",javaType=String.class),
		@Result(property="type",column="type",javaType=String.class),
		@Result(property="wxopenid",column="wxopenid",javaType=String.class)
	})
	public UserBean queryById(String id) throws Exception;
	
	@Select("select * from sys_user")
	@Results({
		@Result(id=true,property="id",column="id",javaType=String.class),
		@Result(property="username",column="username",javaType=String.class),
		@Result(property="password",column="password",javaType=String.class),
		@Result(property="account",column="account",javaType=String.class),
		@Result(property="job",column="job",javaType=String.class),
		@Result(property="adduser",column="adduser",javaType=String.class),
		@Result(property="remark",column="remark",javaType=String.class),
		@Result(property="isuse",column="isuse",javaType=String.class),
		@Result(property="addtime",column="addtime",javaType=Date.class),
		@Result(property="wxnumber",column="wxnumber",javaType=String.class),
		@Result(property="tel",column="tel",javaType=String.class),
		@Result(property="type",column="type",javaType=String.class),
		@Result(property="wxopenid",column="wxopenid",javaType=String.class)
	})
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public List<UserBean> selectAllUser();

	public void batchDeletes(@Param("array")Object...list);

	@Update("UPDATE sys_user SET password=#{pwd} WHERE id=#{id}")
	public void initPwd(@Param("id")String id,@Param("pwd")String pwd);

	public UserBean findByAccount(@Param("account")String account);

	public UserBean findByAccountID(@Param("id")String id, @Param("account")String account);
	
	@Select("select * from sys_user where account=#{account}")
	@Results({
		@Result(id=true,property="id",column="id",javaType=String.class),
		@Result(property="username",column="username",javaType=String.class),
		@Result(property="password",column="password",javaType=String.class),
		@Result(property="account",column="account",javaType=String.class),
		@Result(property="type",column="type",javaType=String.class),
		@Result(property="adduser",column="adduser",javaType=String.class),
		@Result(property="remark",column="remark",javaType=String.class),
		@Result(property="isuse",column="isuse",javaType=String.class),
		@Result(property="addtime",column="addtime",javaType=Date.class),
		@Result(property="wxnumber",column="wxnumber",javaType=String.class),
		@Result(property="tel",column="tel",javaType=String.class),
		@Result(property="type",column="type",javaType=String.class),
		@Result(property="wxopenid",column="wxopenid",javaType=String.class)
	})
	public Map getUserName(@Param("account")String account);

	@Update("UPDATE sys_user SET isuse=#{type} WHERE id=#{id}")
	public void UseStatus(@Param("id")String id, @Param("type")String type);

}